/**
 * Created by zycool on 2016/11/11.
 * 外检数据对齐 - 完整性分析
 */


;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点
    var $begin = $('#JQ_begin');
    // 终点
    var $end = $('#JQ_end');
    // 外检项目1
    var $task_01 = $('#JQ_task_01').select3({
        search: false
    });
    // 外检项目2
    var $task_02 = $('#JQ_task_02').select3({
        search: false
    }).disable();
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                if (dataset.bool) {
                    $begin.val(dataset.result.beginMileage);
                    $end.val(dataset.result.endMileage);
                }
            });
            // 外检项目
            DyyBase.ajax(analysisApi.url_outside_task(), {pipeLineId: id}, function (dataset) {
                if (dataset.bool) {
                    var json = DyyBase.getSelect(dataset.result);
                    $task_01.add(json);
                }
            });
        } else {
            $begin.val('');
            $end.val('');
            $segment.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                if (dataset.bool) {
                    $begin.val(dataset.result.beginMileage);
                    $end.val(dataset.result.endMileage);
                }
            });
        } else {
            $begin.val('');
            $end.val('');
        }
    });

    //
    $task_01.element.on('select', function (e, v){
        var pipeId = $pipeline.val();
        var id = v || $task_01.val();
        if (id) {
            // 外检项目
            DyyBase.ajax(analysisApi.url_outside_task(), {pipeLineId: pipeId,excludeExinspTaskId:id}, function (dataset) {
                if (dataset.bool) {
                    var json = DyyBase.getSelect(dataset.result);
                    $task_02.add(json);
                }
            });
        }
    });

    // 查询对象
    var filter = {
        getSearch: function () {
            var data = {};
            data.mode = true;
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({
                        content: '管道名称不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                } else {
                    data.mode = false;
                }
                return '';
            }();
            data.startMileage = function () {
                var v = $.trim($begin.val());
                if(v){
                    if(isNaN(v)){
                        $begin.TIP({
                            content: '起点距离须为数字!',
                            zindex: 9
                        });
                        data.mode = false;
                    }else{
                        v = parseFloat(v);
                        if(v >= 0){
                            return v;
                        }else{
                            $begin.TIP({
                                content: '起点距离须大于或等于0!',
                                zindex: 9
                            });
                            data.mode = false;
                        }
                    }
                }else{
                    $begin.TIP({
                        content: '起点距离不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.endMileage = function () {
                var v1 = $.trim($begin.val());
                var v2 = $.trim($end.val());
                if (v2) {
                    if (isNaN(v2)) {
                        $end.TIP({
                            content: '终点距离须为数字!',
                            zindex: 9
                        });
                        data.mode = false;
                    } else {
                        v1 = parseFloat(v1);
                        v2 = parseFloat(v2);
                        if(v2 <= v1){
                            $end.TIP({
                                content: '终点距离须大于起点距离!',
                                zindex: 9
                            });
                            data.mode = false;
                        }else{
                            return v2;
                        }
                    }
                } else {
                    $end.TIP({
                        content: '终点距离不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.exinspTaskId1 = function () {
                var v = $task_01.val();
                if (DyyBase.notEmpty(v)) {
                    return v;
                } else {
                    $task_01.element.TIP({
                        content: '外检项目1不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.exinspTaskId2 = function () {
                var v = $task_02.val();
                if (DyyBase.notEmpty(v)) {
                    return v;
                } else {
                    $task_02.element.TIP({
                        content: '外检项目2不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            return data;
        }
    };


    // 1.PCM+ACVG(漏损点分布)
    var pcm_acvg = {
        exinspTask1:[
            {mileage: 0, dbValue: 0},
            {mileage: 1000, dbValue: 10},
            {mileage: 2000, dbValue: 20},
            {mileage: 3000, dbValue: 30},
            {mileage: 4000, dbValue: 40},
            {mileage: 5000, dbValue: 50},
            {mileage: 6000, dbValue: 60},
            {mileage: 7000, dbValue: 70},
            {mileage: 12000, dbValue: 80}
        ],
        exinspTask2:[
            {mileage: 0, dbValue: 45},
            {mileage: 1000, dbValue: 89},
            {mileage: 2000, dbValue: 100},
            {mileage: 3000, dbValue: 200},
            {mileage: 4000, dbValue: 424},
            {mileage: 5000, dbValue: 201},
            {mileage: 6000, dbValue: 322},
            {mileage: 7000, dbValue: 240},
            {mileage: 12000, dbValue: 402}
        ]
    };

    // 2.测试桩管地电位(自然电位)
    var cips_dvcg_ground = [
        [
            {x: 0, y: 0},
            {x: 2000, y: 120},
            {x: 4000, y: 100},
            {x: 6000, y: 160},
            {x: 8000, y: 120},
            {x: 12000, y: 180},
            {x: 16000, y: 100},
            {x: 20000, y: 200},
            {x: 22000, y: 120}
        ],
        [
            {x: 0, y: 200},
            {x: 2000, y: 320},
            {x: 4000, y: 300},
            {x: 6000, y: 360},
            {x: 8000, y: 320},
            {x: 12000, y: 380},
            {x: 16000, y: 300},
            {x: 20000, y: 400},
            {x: 22000, y: 320}
        ]
    ];

    // 3.测试桩管地电位(通电电位)
    var cips_dvcg_ir = [
        {x: 0, y: 0},
        {x: 2000, y: 120},
        {x: 4000, y: 100},
        {x: 6000, y: 160},
        {x: 8000, y: 120},
        {x: 12000, y: 180},
        {x: 16000, y: 100},
        {x: 20000, y: 200},
        {x: 22000, y: 120}
    ];

    // 4.测试桩管地电位(断电/极化电位)
    var test_ground = [
        [
            {x: 0, y: 0},
            {x: 2000, y: 120},
            {x: 4000, y: 100},
            {x: 6000, y: 160},
            {x: 8000, y: 120},
            {x: 12000, y: 180},
            {x: 16000, y: 100},
            {x: 20000, y: 200},
            {x: 22000, y: 120}
        ],
        [
            {x: 0, y: 200},
            {x: 2000, y: 320},
            {x: 4000, y: 300},
            {x: 6000, y: 360},
            {x: 8000, y: 320},
            {x: 12000, y: 380},
            {x: 16000, y: 300},
            {x: 20000, y: 400},
            {x: 22000, y: 320}
        ],
        [
            {x: 0, y: 400},
            {x: 2000, y: 520},
            {x: 4000, y: 500},
            {x: 6000, y: 560},
            {x: 8000, y: 520},
            {x: 12000, y: 580},
            {x: 16000, y: 500},
            {x: 20000, y: 600},
            {x: 22000, y: 520}
        ]
    ];

    // 5.CIPS+DCVG(通电电位)
    var ellipse = [
        {x: 0, y: '00:00', ys: 1},
        {x: 1000, y: '01:00', ys: 2},
        {x: 5000, y: '02:00', ys: 3},
        {x: 8000, y: '03:00', ys: 1},
        {x: 10000, y: '04:00', ys: 2},
        {x: 15000, y: '05:00', ys: 3},
        {x: 18000, y: '06:00', ys: 1},
        {x: 19000, y: '07:00', ys: 2},
        {x: 20000, y: '08:00', ys: 3},
        {x: 22000, y: '12:00', ys: 1}
    ];

    // 6.CIPS+DCVG(断电电位)
    var hww = [
        {x: 0, y: '00:00', ys: 1},
        {x: 1000, y: '01:00', ys: 2},
        {x: 5000, y: '02:00', ys: 1},
        {x: 8000, y: '03:00', ys: 1},
        {x: 10000, y: '04:00', ys: 2},
        {x: 15000, y: '05:00', ys: 2},
        {x: 18000, y: '06:00', ys: 2},
        {x: 19000, y: '07:00', ys: 1},
        {x: 20000, y: '08:00', ys: 1},
        {x: 22000, y: '12:00', ys: 2}
    ];

    // 7.CIPS+DCVG(IR%)
    var metal = [
        {x: 0, y: '00:00'},
        {x: 1000, y: '01:00'},
        {x: 5000, y: '02:00'},
        {x: 8000, y: '03:00'},
        {x: 10000, y: '04:00'},
        {x: 15000, y: '05:00'},
        {x: 18000, y: '06:00'},
        {x: 19000, y: '07:00'},
        {x: 20000, y: '08:00'},
        {x: 22000, y: '12:00'}
    ];

    // 8.管道基础信息
    var pipeinfo = [
        {mile: 0, type: 1},
        {mile: 2000, type: 2},
        {mile: 8000, type: 1},
        {mile: 10000, type: 2},
        {mile: 16000, type: 1},
        {mile: 22000, type: 2}
    ];


    // 查询
    $('#JQ_search').click(function () {
        var $this = $(this);
        var param = filter.getSearch();
        // param.mode
        if (param.mode) {
            $this.attr('data-search', 'true');
            var mile = DyyBase.getMinMax(param.startMileage, param.endMileage);
            var min = mile.min, max = mile.max;

            // 1.PCM+ACVG(漏损点分布)
            DyyBase.ajax(analysisApi.url_outside_pcm_acvg(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result.exinspTask1.length || dataset.result.exinspTask2.length) {
                        var task1 = d3Charts.formart(dataset.result.exinspTask1, {x: 'mileage2', y: 'dbValue'}, null, 'task1');
                        var task2 = d3Charts.formart(dataset.result.exinspTask2, {x: 'mileage2', y: 'dbValue'}, null, 'task2');
                        var alltask = task1.concat(task2);
                        d3Charts.point({
                            container: 'JQ_pcm_acvg',
                            color: ['#48a2e8','#f09191'],
                            data: alltask,
                            areaShow: true,
                            tip: {
                                xtag: '绝对距离(m)',
                                ytag: 'dB值'
                            },
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                data: [],
                                ticks: 5
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            },
                            circleRender: function(d){
                                switch (d.ys){
                                    case 'task1':
                                        return '#48a2e8';
                                        break;
                                    case 'task2':
                                        return '#f09191';
                                        break;
                                    default:
                                        return '#48a2e8';
                                        break;
                                }
                            }
                        });
                        $('#JQ_pcm_acvg').trigger('playTag','true');
                    } else {
                        $('#JQ_pcm_acvg').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 测试桩管地电位 3 个
            DyyBase.ajax(analysisApi.url_outside_test_ground(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result) {
                        if (dataset.result.exinspTask1.length || dataset.result.exinspTask2.length) {
                            var free1 = d3Charts.formart(dataset.result.exinspTask1, {x: 'mileage', y: 'freeCorrosionPotential'});
                            var free2 = d3Charts.formart(dataset.result.exinspTask2, {x: 'mileage', y: 'freeCorrosionPotential'});
                            var all_free = [free1, free2];

                            var on1 = d3Charts.formart(dataset.result.exinspTask1, {x: 'mileage',y: 'onPotentialAvg'});
                            var on2 = d3Charts.formart(dataset.result.exinspTask2, {x: 'mileage',y: 'onPotentialAvg'});
                            var all_on = [on1, on2];

                            var off1 = d3Charts.formart(dataset.result.exinspTask1, {x: 'mileage',y: 'offPotentialAvg'});
                            var off2 = d3Charts.formart(dataset.result.exinspTask2, {x: 'mileage',y: 'offPotentialAvg'});
                            var all_off = [off1, off2];

                            //console.log(all_free);

                            // 2.测试桩管地电位(自然电位)
                            if (all_free.length) {
                                d3Charts.dogLeg({
                                    container: 'JQ_test_free',
                                    color: ['#48a2e8', '#f09191'],
                                    data: all_free,
                                    xAxis: {
                                        data: [min, max],
                                        ticks: 10
                                    },
                                    yAxis: {
                                        ticks: Infinity
                                    },
                                    layout: {
                                        margin: {
                                            left: 50,
                                            right: 30,
                                            top: 10,
                                            bottom: 20
                                        },
                                        xtag: '',
                                        ytag: '',
                                        ygrid: true
                                    },
                                    tip: {
                                        show: true,
                                        xtag: '绝对距离(m)：',
                                        ytag: ['项目1电位：','项目2电位：']
                                    }
                                });
                                $('#JQ_test_free').trigger('playTag','true');
                            } else {
                                $('#JQ_test_free').trigger('clear').trigger('playTag','false');
                            }

                            // 3.测试桩管地电位(通电电位)
                            if (all_on[0].length || all_on[1].length) {
                                d3Charts.dogLeg({
                                    container: 'JQ_test_on',
                                    color: ['#48a2e8', '#f09191'],
                                    data: all_on,
                                    xAxis: {
                                        data: [min, max],
                                        ticks: 10
                                    },
                                    yAxis: {
                                        ticks: Infinity
                                    },
                                    layout: {
                                        margin: {
                                            left: 50,
                                            right: 30,
                                            top: 10,
                                            bottom: 20
                                        },
                                        xtag: '',
                                        ytag: '',
                                        ygrid: true
                                    },
                                    tip: {
                                        show: true,
                                        xtag: '绝对距离(m)：',
                                        ytag: ['项目1电位：','项目2电位：']
                                    }
                                });
                                $('#JQ_test_on').trigger('playTag','true');
                            } else {
                                $('#JQ_test_on').trigger('clear').trigger('playTag','false');
                            }

                            // 4.测试桩管地电位(断电/极化电位)
                            if (all_off[0].length || all_off[1].length) {
                                d3Charts.dogLeg({
                                    container: 'JQ_test_off',
                                    color: ['#48a2e8', '#f09191'],
                                    data: all_off,
                                    xAxis: {
                                        data: [min, max],
                                        ticks: 10
                                    },
                                    yAxis: {
                                        ticks: Infinity
                                    },
                                    layout: {
                                        margin: {
                                            left: 50,
                                            right: 30,
                                            top: 10,
                                            bottom: 20
                                        },
                                        xtag: '',
                                        ytag: '',
                                        ygrid: true
                                    },
                                    tip: {
                                        show: true,
                                        xtag: '绝对距离(m)：',
                                        ytag: ['项目1电位：','项目2电位：']
                                    }
                                });
                                $('#JQ_test_off').trigger('playTag','true');
                            } else {
                                $('#JQ_test_off').trigger('clear').trigger('playTag','false');
                            }
                        }
                    }
                }
            });

            // CIPS+DCVG 3 个
            DyyBase.ajax(analysisApi.url_outside_cips_dcvg(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result) {
                        if (dataset.result.exinspTask1 || dataset.result.exinspTask2) {

                            var on1 = d3Charts.formart(dataset.result.exinspTask1.cipsOnPotentialList, {x: 'mileage2',y: 'cipsOnPotential'});
                            var on2 = d3Charts.formart(dataset.result.exinspTask2.cipsOnPotentialList, {x: 'mileage2',y: 'cipsOnPotential'});
                            var all_on = [on1, on2];

                            var off1 = d3Charts.formart(dataset.result.exinspTask1.cipsOffPotentialList, {x: 'mileage2',y: 'cipsOffPotential'});
                            var off2 = d3Charts.formart(dataset.result.exinspTask2.cipsOffPotentialList, {x: 'mileage2',y: 'cipsOffPotential'});
                            var all_off = [off1, off2];

                            var ir1 = d3Charts.formart(dataset.result.exinspTask1.dcvgIrList, {x: 'mileage2', y: 'dcvgIr'});
                            var ir2 = d3Charts.formart(dataset.result.exinspTask2.dcvgIrList, {x: 'mileage2', y: 'dcvgIr'});
                            var all_ir = [ir1, ir2];

                            // 5.CIPS+DCVG(通电电位)
                            if (all_on[0].length || all_on[1].length) {
                                d3Charts.dogLeg({
                                    container: 'JQ_cips_dcvg_on',
                                    color: ['#48a2e8', '#f09191'],
                                    data: all_on,
                                    xAxis: {
                                        data: [min, max],
                                        ticks: 10
                                    },
                                    yAxis: {
                                        ticks: Infinity
                                    },
                                    layout: {
                                        margin: {
                                            left: 50,
                                            right: 30,
                                            top: 10,
                                            bottom: 20
                                        },
                                        xtag: '',
                                        ytag: '',
                                        ygrid: true
                                    },
                                    tip: {
                                        show: true,
                                        xtag: '绝对距离(m)：',
                                        ytag: ['项目1电位：','项目2电位：']
                                    }
                                });
                                $('#JQ_cips_dcvg_on').trigger('playTag','true');
                            } else {
                                $('#JQ_cips_dcvg_on').trigger('clear').trigger('playTag','false');
                            }

                            // 6.CIPS+DCVG(断电电位)
                            if (all_off[0].length || all_off[1].length) {
                                d3Charts.dogLeg({
                                    container: 'JQ_cips_dcvg_off',
                                    color: ['#48a2e8', '#f09191'],
                                    data: all_off,
                                    xAxis: {
                                        data: [min, max],
                                        ticks: 10
                                    },
                                    yAxis: {
                                        data: [],
                                        ticks: Infinity
                                    },
                                    layout: {
                                        margin: {
                                            left: 50,
                                            right: 30,
                                            top: 10,
                                            bottom: 20
                                        },
                                        xtag: '',
                                        ytag: '',
                                        ygrid: true
                                    },
                                    tip: {
                                        show: true,
                                        xtag: '绝对距离(m)：',
                                        ytag: ['项目1电位：','项目2电位：']
                                    }
                                });
                                $('#JQ_cips_dcvg_off').trigger('playTag','true');
                            } else {
                                $('#JQ_cips_dcvg_off').trigger('clear').trigger('playTag','false');
                            }

                            // 7.CIPS+DCVG(IR%)
                            if (all_ir[0].length || all_ir[1].length) {
                                d3Charts.dogLeg({
                                    container: 'JQ_cips_dcvg_ir',
                                    color: ['#48a2e8', '#f09191'],
                                    data: all_ir,
                                    xAxis: {
                                        data: [min, max],
                                        ticks: 10
                                    },
                                    yAxis: {
                                        data: [],
                                        ticks: Infinity
                                    },
                                    layout: {
                                        margin: {
                                            left: 50,
                                            right: 30,
                                            top: 10,
                                            bottom: 20
                                        },
                                        xtag: '',
                                        ytag: '',
                                        ygrid: true
                                    },
                                    tip: {
                                        show: true,
                                        xtag: '绝对距离(m)',
                                        ytag: ['项目1IR%：','项目2IR%：']
                                    }
                                });
                                $('#JQ_cips_dcvg_ir').trigger('playTag','true');
                            } else {
                                $('#JQ_cips_dcvg_ir').trigger('clear').trigger('playTag','false');
                            }
                        }
                    }
                }
            });

            // 8.管道基础信息
            DyyBase.ajax(analysisApi.url_pipe_mark(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var data = d3Charts.formartYs(dataset.result,{x:'MILEAGE',y:'TYPE'});
                        d3Charts.signSx({
                            container: 'JQ_pipeinfo',
                            color: ['#f09091', '#7eb9e7'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            pointText: {
                                show: false
                            }
                        });
                        $('#JQ_pipeinfo').trigger('playTag','true');
                    }else{
                        $('#JQ_pipeinfo').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 启用拖拽
            $('#JQ_pdf_area').dragLine();
        }
    });

    // 导出
    $('#JQ_export').click(function () {
        var $this = $(this);
        DyyBase.isLogin(function(){
            var param = filter.getSearch();
            var search = $('#JQ_search').attr('data-search');
            if (param.mode) {
                if (search === 'true') {
                    $this.EXPT({
                        type: 'name',
                        name: param.pipeLineName + '外外检数据对齐分析图',
                        yesBtnAct: function (callback,fname) {
                            callback();
                            canvasPdf.exportHtmlPdf({
                                dom: $('#JQ_pdf_area'),
                                server: DyyBase.root_path + '/comm/downImgPdf.json',
                                param: {
                                    fileName: fname,
                                    name: fname
                                }
                            });
                        }
                    });
                } else {
                    $('#JQ_search').MSG({
                        content: '尚未绘制图形,请先执行查询!'
                    });
                }
            }
        });
    });


})(jQuery, window, document);